using System.Text.Encodings.Web;
using System.Text.Unicode;
using blog.Entity;
using blog.Service;
using blog.web.Middlewares;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args).Inject();

builder.Services.AddControllers().AddInject().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
}).AddMvcOptions((opt) =>
{
    opt.Filters.Add<ApiAuthorizeAttribute>();
});;

// Add services to the container.

builder.Services.AddControllers();

builder.Services.AddCorsAccessor();

ILoggerFactory logger = LoggerFactory.Create(builder =>
{
    builder.AddConsole();
});

builder.Services.AddDbContext<BlogContext>(opt =>
{
    string conStr = builder.Configuration.GetConnectionString("MssqlConStr");
    opt.UseSqlServer(conStr);
    opt.UseLoggerFactory(logger);
});


builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IGroupService, GroupService>();


var app = builder.Build();

app.UseMiddleware<JwtMiddleware>();
app.UseMiddleware<RealIpMiddleware>();

app.UseCorsAccessor();

app.UseAuthorization();

app.UseInject();

app.MapControllers();

app.Run();